/**
  * @file		main_serial.h
  * @author		Naresuan University: Advance Computer System, class of 2013
  * @version	V1.0.0
  * @date		18-September-2013
  * @brief		Main program for testing serial module
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "serial.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Board_config(void);

/* Private functions ---------------------------------------------------------*/
void Board_config() 
{
	Serial_begin(9600);
}


/**
  * @brief		Main program: configure on-board hardware, then main loop
  * @param		None
  * @retval		None
  */
int main()
{
	char two[] = "two";
	char sentence[] = "3. This is very long sentence, so that it will fill up the whole buffer.\n";
	char msg[] = "Type something:";
	int i;
	
	Board_config();
	// 1. test writing one byte
	Serial_write('1');
	Serial_write('\n');
	
	// 2. test writing continuously
	for (i = 0; i < sizeof(two); i++) {
		Serial_write(two[i]);
	}
	Serial_write('\n');
	
	// 3. test writing long sentence
	for (i = 0; i < sizeof(sentence); i++) {
		Serial_write(sentence[i]);
	}
	Serial_write('\n');

	// 4. test flushing buffer
	Serial_flush();
	Serial_write('\n');
	
	// 5. test waiting for one coming byte
	for (i = 0; i < sizeof(msg); i++) {
		Serial_write(msg[i]);
	}
	while (Serial_available() == 0);
	Serial_write('\n');
	
	// 6. test reading one byte
	Serial_write(Serial_read());
	Serial_write('\n');
	
	// 7. test waiting for 5-byte message
	while (Serial_available() < 5);
	for (i = 0; i < 5; i++) {
		Serial_write(Serial_read());
	}
	Serial_write('\n');
	
	while(1);
}
